package jwebtest;

import java.sql.SQLException;
import java.util.Arrays;

import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;
import org.htmlparser.util.SimpleNodeIterator;

import com.jweb.plugin.jdbc.JdbcTemplate;
import com.jweb.util.FormatUtil;

public class JWebRobot {
	private JdbcTemplate jdbc;

	public JWebRobot(JdbcTemplate jdbc) {
		this.jdbc = jdbc;
	}

	public static void main(String[] args) throws ClassNotFoundException {
		JdbcTemplate jd = new JdbcTemplate("jdbc:mysql://localhost:3306/", "root", "root");

		boolean bo = true;
		int id = 32000;
		int err = 0;
		while (bo) {
			try {
				JWebRobot jr = new JWebRobot(jd);
				jr.getNews(id++);
				err = 0;
			} catch (Exception e) {
				err++;
				e.printStackTrace();
				if (err > 20)
					bo = false;
			}
		}
	}

	public void getNews(int id) throws ParserException, SQLException {
		String website_url = "http://www.paynews.net/article-" + id + "-1.html";
		// String website_url = "http://www.paynews.net/article-28804-1.html";
		Parser parser = getWebHtml(website_url, "GBK");
		System.out.println("[NEWS_ID:]" + id);
		System.out.println("[WEBSITE_NAME:]中国支付网");
		String title = getElement(parser, "class", "biaoti").elementAt(0).toPlainTextString();
		System.out.println("[TITLE:]" + title);

		NodeList sni = getElement(parser, "class", "z");
		System.out.println("【type】:" + sni.elementAt(1).getChildren().elementAt(17).toPlainTextString());
		String type = sni.elementAt(1).getChildren().elementAt(17).toPlainTextString();

		String from = getElement(parser, "class", "from").elementAt(0).toPlainTextString();
		System.out.println("[FROM:]" + from);
		String create_time = from.substring(0, 16);

		String[] time = create_time.split(" ");
		String[] date = time[0].split("-");
		create_time = (date[0] + "-" + FormatUtil.leftPad(date[1], 2,"0") + "-" + FormatUtil.leftPad(date[2], 2,"0") + " " + FormatUtil.toStringTrim(time[1]))
				.substring(0, 16);
		System.out.println("[Create_time]:[" + create_time + "]");
		String abstracts = getElement(parser, "class", "zhaiyao").elementAt(0).toPlainTextString().replaceAll("\\s+", "");
		abstracts=abstracts.replace("摘要:&nbsp;&nbsp;", "").trim();
		System.out.println("[ABSTRACT:]" + abstracts);
		String content = getElement(parser, "class", "content").elementAt(0).toHtml(false);
		content = content.replaceAll("<!--\\[diy=diycontenttop\\]-->(.*)<!--\\[/diy\\]-->", "");
		content = content.replaceAll("<a[^>]*>|</a>", "");
		content = content.replaceAll("src=\"data", "src=\"http://www.paynews.net/data");
		String sbb = content.toString();
		String str = "";
		int a = sbb.indexOf("img");
		if (a < 0)
			a = sbb.indexOf("IMG");
		if (a > 0) {
			try {
				str = sbb.substring(a);
				int b = str.indexOf(">");
				str = str.substring(0, b);
				str = str.substring(str.indexOf("http"));
				str = str.substring(0, str.indexOf("\""));
				System.out.println("[IMG:]" + str);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		System.out.println("【context:】" + sbb);
		jdbc.openTransaction();
		try {
			++index;
			jdbc.update(
					"INSERT INTO home_web.news    (id,websiteName,websiteUrl,title,abstracts,createTime,type,img) VALUES (?,?,?,?,?,?,?,?)",
					new Object[] { index, "中国支付网", website_url.trim(), title.trim(), abstracts.trim(), create_time.trim(), type.trim(), str.trim() });
			jdbc.update("INSERT INTO home_web.news_detail(id,text) VALUES(?,?)", new Object[] { index, sbb.trim() });
			jdbc.commit();
		} catch (Exception e) {
			index--;
			jdbc.rollback();
			System.err.println("================SQL以回滚================");
			e.printStackTrace();
		}

	}

	private static int index = 0;

	public NodeList getElement(Parser parser, String tag, String name) throws ParserException {
		parser.reset();// 重置
		NodeFilter filter = new HasAttributeFilter(tag, name);
		return parser.extractAllNodesThatMatch(filter);
	}

	/** 获取网页完整内容 */
	public Parser getWebHtml(final String htmlurl, String Encoding) throws ParserException {
		// StringBuffer sb =new StringBuffer(10240);
		// try {
		// URL url = new URL(htmlurl);
		// URLConnection context = url.openConnection();
		// InputStream in = context.getInputStream();
		// BufferedReader br = new BufferedReader(new InputStreamReader(in,
		// "gbk"));
		// String line;
		// while (( line = br.readLine()) != null) {
		// sb.append(line);
		// }
		// } catch (Exception e) {
		// e.printStackTrace();
		// }
		Parser p = new Parser(htmlurl);
		p.setEncoding(Encoding);
		return p;
	}

}
